//
//  UserCenterViewController.m
//  ChineseCollegeStudentsNetwork
//
//  Created by 刘喆 on 2018/5/10.
//  Copyright © 2018年 刘喆. All rights reserved.
//

#import "UserCenterViewController.h"
#import "UserCenterTableViewCell.h"
#import "UserCenterHeaderView.h"
#import "LoginViewController.h"
#import "AllOrdersListViewController.h"
#import "AccountSecurityViewController.h"

#import "PersonalInformationViewController.h"
#import "ShoppingCartViewController.h"
#import "AddressViewController.h"
#import "AllOrdersListViewController.h"
#import "OrderListViewController.h"
#import "InformationListViewController.h"

static NSString *const MineTableViewCell_ID = @"MineTableViewCell_ID";

@interface UserCenterViewController ()<UITableViewDelegate,UITableViewDataSource>

@property (nonatomic, strong) UITableView *tableView;//tableView
@property (nonatomic, strong) UserCenterHeaderView *headerView;//

@property (nonatomic, strong) NSArray<NSDictionary *> *arr;



@end

@implementation UserCenterViewController

- (instancetype)init {
    self = [super init];
    if (self) {
        _arr = @[@{@"titleName":@"个人信息",@"imageName":@"个人信息icon"},
                                         @{@"titleName":@"购物车",@"imageName":@"购物车icon"},
                                         @{@"titleName":@"我的订单",@"imageName":@"我的订单icon"},
                                        @{@"titleName":@"收获地址",@"imageName":@"收获地址icon"},
                                         @{@"titleName":@"账号安全",@"imageName":@"账号安全icon"},];

    }
    return self;
}

#pragma mark - Life Cycle

- (void)viewDidLoad {
    [super viewDidLoad];
    
    self.title = @"用户中心";
    
    [self setupConstraints];
}

- (void)viewWillAppear:(BOOL)animated {
    [super viewWillAppear:animated];
    
    
    [MLAHttpManage postHttpType:MLAHttpRequestType_Get hostType:MLAHttpHostType_Api api:[self getUserIdApi:@"/api/v1/users"] params:nil success:^(id  _Nonnull response, NSString * _Nonnull msg) {
        UserModel *model = [[UserModel alloc] initWithDictionary:response error:nil];
        [UserManager sharedInstance].userModel = model;
        _headerView.name.text = [NSString stringWithFormat:@"%@,欢迎",model.value.displayName];
    } failure:^{
        
    }];
}

- (void)setupConstraints {
    
    [self.view addSubview:self.tableView];
    
    [self.tableView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.edges.offset(0);
    }];
    
}

- (UITableView *)tableView {
    if (!_tableView) {
        _tableView = [[UITableView alloc] initWithFrame:CGRectZero style:UITableViewStyleGrouped];
        _tableView.backgroundColor = UIColor.whiteColor;
        _tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
        _tableView.delegate = self;
        _tableView.dataSource = self;
        [_tableView registerClass:[UserCenterTableViewCell class] forCellReuseIdentifier:MineTableViewCell_ID];
        _tableView.tableHeaderView = self.headerView;
        if (@available(iOS 11.0, *)) {
            _tableView.contentInsetAdjustmentBehavior = UIScrollViewContentInsetAdjustmentNever;
        } else {
            self.automaticallyAdjustsScrollViewInsets = NO;
        }
        
    }
    return _tableView;
}

- (UserCenterHeaderView *)headerView {
    if (!_headerView) {
        _headerView = [[UserCenterHeaderView alloc] initWithFrame:CGRectMake(0, 0, CGRectGetWidth(self.view.bounds), 200)];
    
    }
    return _headerView;
}

#pragma mark -
#pragma mark tableview datasource
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
    return 5;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
   
    return 1;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    UserCenterTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:MineTableViewCell_ID forIndexPath:indexPath];
    cell.iconImgv.image = [UIImage imageNamed:_arr[indexPath.section][@"imageName"]];
    cell.titleLbl.text = _arr[indexPath.section][@"titleName"];
    
    return cell;
}

#pragma mark tableview delegate
- (CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section {
    if (section == 3) {
        return 15;
    } else {
        return 5;
    }
    
}

- (UIView *)tableView:(UITableView *)tableView viewForFooterInSection:(NSInteger)section {
    if (section != 4) {
        UIView *footView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, kDeviceWidth, 5)];
        footView.backgroundColor = kRGB(245, 245, 245, 1);
        return footView;
    } else {
        return nil;
    }
    
}

- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section {
    return .0001f;
}

- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section {
    return nil;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    
    switch (indexPath.section) {
        case 0:
            {
                PersonalInformationViewController *PIVC = [[PersonalInformationViewController alloc] init];
                [self.navigationController pushViewController:PIVC animated:YES];
                
            }
            break;
        case 1:
        {
                ShoppingCartViewController *SCVC = [[ShoppingCartViewController alloc] init];
                [self.navigationController pushViewController:SCVC animated:YES];
        }
            break;
        case 2:
        {
            AllOrdersListViewController *aolVC = [[AllOrdersListViewController alloc] init];
            [self.navigationController pushViewController:aolVC animated:YES];

        }
            break;
        case 3:
        {
            AddressViewController *address = [[AddressViewController alloc] init];
            [self.navigationController pushViewController:address animated:YES];

        }
            break;
        case 4:
        {
            AccountSecurityViewController *asVC = [[AccountSecurityViewController alloc] init];
            [self.navigationController pushViewController:asVC animated:YES];
        }
            break;
            
        default:
            break;
    }
    
    
}


- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}



@end
